﻿
/// <reference path="../../Plugin/datepicker/js/jquery.ui.datepicker.js" />
/// <reference path="../../Plugin/datepicker/css/jquery.ui.core.css" />
/// <reference path="../../Plugin/datepicker/css/jquery.ui.datepicker.css" />
/// <reference path="../../Plugin/datepicker/css/jquery.ui.theme.css" />
/// <reference path="../Pages/Product/BuyingGroup.js" />



//-----------------------------------------------------------
// Author:      thuphuong.tran
// Create Date: 28/10/2010
// Description: Xử lý show popup danh sach Admin
//-----------------------------------------------------------
ShowListItem = new function () {

    this.itemID = 0;
    this.itemCode = -1;
    this.fromDate = "";
    this.toDate = "";
    this.name = "";
    this.categoryID = -1;
    this.status = -1;
    this.m_objPageListItem = new VtcPaging("divPagingListItem", "ShowListItem.bindListItem", "pagingCss", 5, CONSTANT.PAGE_DISPLAY);
    this.m_idShowListItem = "divShowListItem";
    this.showPopup = function () {

        if ($("#" + this.m_idShowListItem).length == 0) {
            $("body").append("<div id='" + this.m_idShowListItem + "'></div>");
            $("#" + ShowListItem.m_idShowListItem).setTemplateURL("/Template/Popup/TrutienShop/ShowListItem.htm");
            $("#" + ShowListItem.m_idShowListItem).processTemplate(null);
            ShowListItem.setRowNumber("#" + ShowListItem.m_idShowListItem);
            var top = (($(window).height() / 2) - ($("#divPopup_ShowListItem").outerHeight() / 2)) - 80;
            var left = (($(window).width() / 2) - ($("#divPopup_ShowListItem").outerWidth() / 2));
            if (top < 0) top = 0;
            if (left < 0) left = 0;
            // IE6 fix
            if ($.browser.msie && parseInt($.browser.version) <= 6) top = top + $(window).scrollTop();

            $("#divPopup_ShowListItem").css({
                top: top + 'px',
                left: left + 'px'
            });

            $(".content_info_left", "#divPopup_ShowListItem").css({
                'text-align': 'left',
                'width': 80 + 'px'
            });

            $(".content_info_right", "#divPopup_ShowListItem").css({
                'text-align': 'left',
                'width': 120 + 'px'
            });

            $("#txtFromDateListItem").datepicker({
                changeMonth: true,
                changeYear: true
            });
            $("#txtToDateListItem").datepicker({
                changeMonth: true,
                changeYear: true
            });

            var _date = new Date();
            //bind dữ liệu cho ToDate
            $("#txtToDateListItem").val(ShowListItem.convertDate(_date));

            //bind dữ liệu cho FromDate
            _date.setDate(_date.getDate() - 365);
            $("#txtFromDateListItem").val(ShowListItem.convertDate(_date));

            $("#txtItemCodeListItem").val("");
            $("#txtItemCodeListItem").focus();
            this.bindCategory();
            this.bindListItem(1, ShowListItem.m_objPageListItem.pageSize);
        }

        try {
            $("#divPopup_ShowListItem").draggable({ handle: $("#divPopup_ShowListItem .barpopup") });
            $("#divPopup_ShowListItem .barpopup").css({ cursor: 'move' });
        } catch (e) { /* requires jQuery UI draggables */ }


    };

    this.bindCategory = function () {
        $.ajax({
            type: "GET",
            url: "/Handler/TrutienShop/HandlerContent.ashx",
            data: { status: 1, t: 'GetListCategoryAdmin' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                if (typeof (data) != 'undefined') {
                    var _sHtml = '<option value="-1">[--' + MESSAGE.all + '--]</option>';
                    for (var i = 0; i < data.Items.length; i++) {
                        _sHtml += '<option value="' + data.Items[i].CategoryID + '">' + data.Items[i].Name + '</option>';
                    }
                    $("#ddlCategoryListItem").html(_sHtml);
                }
            }
        });
    };


    this.bindListItem = function (_cur, _ps) {
        Loading.showProcess();
        ShowListItem.toDate = $("#txtToDateListItem").val();
        ShowListItem.fromDate = $("#txtFromDateListItem").val();

        $.ajax({
            type: "GET",
            url: "/Handler/TrutienShop/HandlerProduct.ashx",
            data: { itemCode: ShowListItem.itemCode, name: ShowListItem.name, categoryID: ShowListItem.categoryID, fromDate: ShowListItem.fromDate, toDate: ShowListItem.toDate, status: ShowListItem.status, currentPage: _cur, pageSize: _ps, t: 'GetListItemForBuyingGroup' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                ShowListItem.processData(data, _cur);
                Loading.closeProcess();
            }
        });

    };

    this.processData = function (data, _cur) {
        $("#spTotalItem").html('(' + data.TotalRecord.toString() + ')');
        $("#divListItem").setTemplateURL("/Template/Popup/TrutienShop/ShowListItemSub.htm");
        $("#divListItem").processTemplate(data);
        ShowListItem.setStyleRow("#divListItem");
        ShowListItem.m_objPageListItem.bindPaging(_cur, data.TotalRecord);
        if (data.TotalRecord <= ShowListItem.m_objPageListItem.pageSize) {
            $("#divPagingListItem").attr('style', 'display:none');
        }
        else {
            $("#divPagingListItem").attr('style', 'display:block');
        }
    };

    this.searchListItem = function () {
        if (!this.validateSearch()) return;
        ShowListItem.itemCode = $("#txtItemCodeListItem").val();
        ShowListItem.fromDate = $("#txtFromDateListItem").val();
        ShowListItem.toDate = $("#txtToDateListItem").val();
        ShowListItem.status = $("#ddlStatusListItem").val();
        ShowListItem.name = $("#txtItemNameListItem").val();
        ShowListItem.categoryID = $("#ddlCategoryListItem").val();
        ShowListItem.bindListItem(1, ShowListItem.m_objPageListItem.pageSize);
    };

    $("#btnSearch").live('click', function () {
        ShowListItem.searchListItem();
    });

    // Kiem tra du lieu truoc khi tim kiem
    this.validateSearch = function () {
        if (!IsNumeric($("#txtItemCodeListItem").val())) {
            jAlert(MESSAGE.erroritemCodeNotIsNumberic, MESSAGE.title, function () {
                $("#txtItemCodeListItem").focus();
            });
            return false;
        }

        if ($("#txtFromDateListItem").val() == '') {
            jAlert(MESSAGE.datetimenull, MESSAGE.title, function () {
                $("#txtFromDateListItem").focus();
            });
            return false;
        }
        if ($("#txtToDateListItem").val() == '') {
            jAlert(MESSAGE.datetimenull, MESSAGE.title, function () {
                $("#txtToDateListItem").focus();
            });
            return false;
        }

        if (Untils.isDate($("#txtFromDateListItem").val()) < 0) {
            jAlert(MESSAGE.errorFormatDate, MESSAGE.title, function () {
                $("#txtFromDateListItem").focus();
            });
            return false;
        }

        if (Untils.isDate($("#txtToDateListItem").val()) < 0) {
            jAlert(MESSAGE.errorFormatDate, MESSAGE.title, function () {
                $("#txtToDateListItem").focus();
            });
            return false;
        }
        if (!Untils.DateCompare($("#txtFromDateListItem").val(), $("#txtToDateListItem").val())) {
            jAlert(MESSAGE.erroDateTime, MESSAGE.title, function () {
                $("#txtToDate").focus();
            });
            return false;
        }
        return true;
    };

    this.validateDigit = function (evt) {
        try {
            var keyCode = evt.keyCode ? evt.keyCode : evt.which;
            var key = String.fromCharCode(keyCode);
            if (key && (/[a-zA-Z]/.test(key) || /[{}=|_<>,\?~`ơư!@#$%^&*().+-]/.test(key))) {
                return (false);
            }
            if (key == "")
                return false;
            return (true);
        } catch (err) {
            return true;
        }
    };

    $("#aSelectItem").live('click', function () {
        _itemID = $(this.parentNode).attr("id").substring(6);
        Pages_ListBuyingGroup.bindItemInfo(_itemID);
        ShowListItem.close();

    });

    this.close = function () {
        if ($("#" + this.m_idShowListItem).length != 0) {
            $("#" + this.m_idShowListItem).remove();
        }
    };

    this.setRowNumber = function (m_selParent) {
        var m_stt = 0;
        $('#divSTT', m_selParent).each(function () {
            m_stt++;
            $(this).text(m_stt);
        });
    };

    this.setStyleRow = function (m_selParent) {
        var m_stt = 0;
        $('#divItem', m_selParent).each(function () {
            m_stt++;
            if ((m_stt % 2) == 0) {
                $(this.parentNode).addClass('even');
            }
            else {
                $(this.parentNode).addClass('odd');
            }
        });
    };

    this.convertDate = function (expDate) {
        return expDate.getDate() + '/' + (expDate.getMonth() + 1) + '/' + expDate.getFullYear();
    };

    this.ConvertGender = function (_gender) {
        if (_gender == 1)
            return "Nam";
        if (_gender == 2)
            return "Nữ";
        if (_gender == 3)
            return "Không xác định";
    };

}